package sg.edu.nus.dashboard.util
{
	import com.as3xls.xls.ExcelFile;
	import com.as3xls.xls.Sheet;
	
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	
	import mx.controls.Alert;
	
	import sg.edu.nus.dashboard.main.model.AppModelLocator;

	public class ReportUtil
	{
		public function ReportUtil()
		{
		}
		
		
		// generate excel report
		public static function genExcelReport():void {
			
			var model:AppModelLocator = AppModelLocator.getInstance();
			var sheet:Sheet;
			var mbytes:ByteArray;
			var file:FileReference;
			var excelFile:ExcelFile = new ExcelFile();	
			
			for (var i:int = 1; i <= model.stat.statRawArr.length; i++) {
				var obj:Object = model.stat.statAllArr[i-1];
				var j:int = 0;
				for ( var key:String in obj) {
					excelFile.sheets.addItem(generateSheet(0,j,key));  
					excelFile.sheets.addItem(generateSheet(i,j,obj[key]));  
					j += 1;
				} 
				
				mbytes = excelFile.saveToByteArray();
				
				file = new FileReference();
				
				try {
					file.save(mbytes,"excelReport.xls");
				} catch (error:Error) {
					trace("Filed:", error.message);
				}
				
				function generateSheet(i:int,j:int,o:Object):Sheet{  
					if(!sheet){  
						sheet = new Sheet();  
						sheet.resize(10,10);  
					}  
					sheet.setCell(i, j, String(o));  
					return sheet;  
				}  
			}
			
		}
		
		// generate pdf report
		public static function genPDFReport():void {
		
		}
	}
}